<?php
namespace app\admin\model;

use frame\libs\BaseModel;

/**
 * 
 */
class UserModel extends BaseModel
{
    protected $table = "rbac_user";

    /**
     * 根据用户ID获取用户信息
     * @param  [type] $username [description]
     * @return [type]           [description]
     */
    public function getUserByUid($uid)
    {
        $params = array(
            'uid' => $uid
        );
        $sql = "SELECT * FROM $this->table WHERE uid=:uid"; 
        return $this->fetchOne($sql, $params);
    }

    /**
     * 根据用户ID获取用户和角色信息
     * @param  [type] $username [description]
     * @return [type]           [description]
     */
    public function getUserWithRoleByUid($uid)
    {
        $params = array(
            'uid' => $uid
        );
        $sql = "SELECT u.*,r.id,r.status AS r_status,r.nodes FROM rbac_user u,rbac_role r WHERE u.uid=:uid AND u.role_id=r.id"; 
        return $this->fetchOne($sql, $params);
    }

    /**
     * 根据用户名获取用户信息
     * @param  [type] $username [description]
     * @return [type]           [description]
     */
    public function getUserByUsername($username)
    {
        $params = array(
            'username' => $username
        );
        $sql = "SELECT * FROM $this->table WHERE username=:username AND status=1"; 
        return $this->fetchOne($sql, $params);
    }

    /**
     * 获取所有用户信息
     * @return [type] [description]
     */
    public function getUsers()
    {
        return $this->fetchAll("SELECT uid FROM $this->table WHERE 1");
    }

    /**
     * 获取当天登录的用户数
     * @return [type] [description]
     */
    public function getUserNumHasLoginToday()
    {
        $today = strtotime(date('Y-m-d'));
        return $this->rowCount("SELECT uid FROM $this->table WHERE last_login>=$today");
    }

    /**
     * 获取所有用户数量
     * @return [type] [description]
     */
    public function getUserNum()
    {
        return $this->rowCount("SELECT * FROM $this->table WHERE 1");
    }

    /**
     * 获取所有用户信息和角色
     * @return [type] [description]
     */
    public function getUsersWithRole() 
    {
        return $this->fetchAll("SELECT a.*,b.role_name, b.status AS b_status FROM $this->table a,rbac_role b WHERE a.role_id=b.id");
    }

    /**
     * 获取所有用户登录次数
     * @return [type] [description]
     */
    public function getUserLoginNum() 
    {
        return $this->fetchAll("SELECT name_zh,login_num FROM $this->table WHERE 1 ORDER BY login_num DESC LIMIT 30");
    }

    /**
     * 更新启用状态
     * @param  int $id     [description]
     * @param  int $status [description]
     * @return bool         [description]
     */
    public function updateStatus($data)
    {   
        try {
            $this->beginTransaction();
            $this->update("UPDATE $this->table SET status=:status,update_time=:update_time WHERE uid=:uid", $data); 
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 删除
     * @param  int $id [description]
     * @return bool     [description]
     */
    public function deleteUserById($uid)
    {
        try {
            $this->beginTransaction();
            $this->update("DELETE FROM $this->table WHERE uid=:uid", array('uid'=>$uid));
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 新增用户
     * @param  array $data [description]
     * @return bool       [description]
     */
    public function insertUser($data)
    {
        try {
            $this->beginTransaction();
            $this->insert($data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 更新用户
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function updateUser($data)
    {
        try {
            $this->beginTransaction();
            $sql = "UPDATE $this->table SET name_zh=:name_zh,username=:username,phone=:phone,status=:status,role_id=:role_id,update_time=:update_time WHERE uid=:uid";
            $this->update($sql, $data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 更新密码
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function updatePassword($data)
    {
        try {
            $this->beginTransaction();
            $sql = "UPDATE $this->table SET password=:password,update_time=:update_time WHERE uid=:uid";
            $this->update($sql, $data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 更新登录时间，登录次数
     * @param  array $data [description]
     * @return [type]       [description]
     */
    public function updateLoginInfo($data)
    {
        try {
            $this->beginTransaction();
            $sql = "UPDATE $this->table SET last_login=:last_login,login_num=login_num+1 WHERE uid=:uid";
            $this->update($sql, $data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 更新用户基本信息：用户名，中文名，手机号
     * @param  array $data [description]
     * @return [type]       [description]
     */
    public function updateUserInfo($data)
    {
        try {
            $this->beginTransaction();
            $sql = "UPDATE $this->table SET username=:username,name_zh=:name_zh,phone=:phone WHERE uid=:uid";
            $this->update($sql, $data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

}

